{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8bceff6e-9047-45cd-a30d-ee8a05ac316a",
   "metadata": {},
   "source": [
    "### 环境配置\n",
    "MindSpore 2.3\n",
    "MindNLP 0.3.1\n",
    "Python 3.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2edff912-48fd-40ec-a79f-b37429c147a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture captured_output\n",
    "!/home/ma-user/anaconda3/bin/conda create -n python-3.9.0 python=3.9.0 -y --override-channels --channel https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main\n",
    "!/home/ma-user/anaconda3/envs/python-3.9.0/bin/pip install ipykernel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "138400a3-55c3-4f18-9b2d-030dc1871018",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "\n",
    "data = {\n",
    "   \"display_name\": \"python-3.9.0\",\n",
    "   \"env\": {\n",
    "      \"PATH\": \"/home/ma-user/anaconda3/envs/python-3.9.0/bin:/home/ma-user/anaconda3/envs/python-3.7.10/bin:/modelarts/authoring/notebook-conda/bin:/opt/conda/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/ma-user/modelarts/ma-cli/bin:/home/ma-user/modelarts/ma-cli/bin\"\n",
    "   },\n",
    "   \"language\": \"python\",\n",
    "   \"argv\": [\n",
    "      \"/home/ma-user/anaconda3/envs/python-3.9.0/bin/python\",\n",
    "      \"-m\",\n",
    "      \"ipykernel\",\n",
    "      \"-f\",\n",
    "      \"{connection_file}\"\n",
    "   ]\n",
    "}\n",
    "\n",
    "if not os.path.exists(\"/home/ma-user/anaconda3/share/jupyter/kernels/python-3.9.0/\"):\n",
    "    os.mkdir(\"/home/ma-user/anaconda3/share/jupyter/kernels/python-3.9.0/\")\n",
    "\n",
    "with open('/home/ma-user/anaconda3/share/jupyter/kernels/python-3.9.0/kernel.json', 'w') as f:\n",
    "    json.dump(data, f, indent=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c0b75ca-1563-4896-aa14-dd54c204a18a",
   "metadata": {},
   "source": [
    "安装完成后重启kernel，选择python3.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11bc67c7-a7be-4f2a-aae5-66ac77ef2816",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture captured_output\n",
    "!pip uninstall mindspore-gpu -y\n",
    "!pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/2.3/MindSpore/unified/x86_64/mindspore-2.3-cp39-cp39-linux_x86_64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "!pip install download nltk -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "!pip install mindnlp"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e618a33-c44e-49ce-a1bc-adc21c1d802f",
   "metadata": {},
   "source": [
    "### 加载数据\n",
    "首先，加载图像数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4891b58b-8b8f-4a53-a7f7-43633da2c52f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Generating train split: 27000 examples [00:05, 4738.60 examples/s]\n"
     ]
    }
   ],
   "source": [
    "from datasets import load_dataset\n",
    "# 数据来源：https://github.com/phelber/EuroSAT\n",
    "dataset = load_dataset(\"imagefolder\", data_files=\"EuroSAT_RGB.zip\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "743c6d59-d2a3-4be6-b0c0-0bc5ef29204b",
   "metadata": {},
   "source": [
    "图像分类任务数据通常包含两类数据：图像和标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "75544b36-3af7-40f1-b53e-2a0d2b4b4ba2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'image': Image(mode=None, decode=True, id=None),\n",
       " 'label': ClassLabel(names=['AnnualCrop', 'Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential', 'River', 'SeaLake'], id=None)}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset[\"train\"].features"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "621ac53a-775f-4e00-a174-9ff3848c4d3a",
   "metadata": {},
   "source": [
    "我们首先可视化一个示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "399fc20a-62a1-4935-90bf-8de847a97eb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABAAEADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkN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S04gMtFpqEpXUR4NQbpVoC5/TXzFRfKxEiucSFIM5dJbtdk3QPTco8bAnzB1R8GyqmADFYUaGL+dDyAYpfogDm8AvNv2OTDGLYvGfRYuaV94a+mJr5iaECGyEAUZmsHM8jXvZyBBdGuiSQsQErFj5dDmMScRe2XtPEyswzmqXGdQ27YbefGlRHBUELwJIjiA0QXnjQnL1yhve6D41RfwDA/dIxrzabK7e5RDzcCYXRhTN0oufg0C0p9S8QGIBidCm6fEeL1NW6ND01Qa02UCMDQRcvqQ7VsiRB0aOyHkJhOKQo8/Vyw0HWBdyCmMqKMNdNUIUPzf9x++opka0AEAAAAAElFTkSuQmCC",
      "text/plain": [
       "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=64x64>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "example = dataset[\"train\"][0]\n",
    "example[\"image\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b7a50755-6b85-419d-8674-8297008d3b36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "example[\"label\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaeb2ab2-6057-4505-87a8-51c5be7cb82b",
   "metadata": {},
   "source": [
    "当前标签展示为整数，我们可以通过如下的方式将它们转化为真实的分类名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ce5c4469-6c26-42a4-a1b0-6d6ad3a9b5ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['AnnualCrop',\n",
       " 'Forest',\n",
       " 'HerbaceousVegetation',\n",
       " 'Highway',\n",
       " 'Industrial',\n",
       " 'Pasture',\n",
       " 'PermanentCrop',\n",
       " 'Residential',\n",
       " 'River',\n",
       " 'SeaLake']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "labels = dataset[\"train\"].features[\"label\"].names\n",
    "labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "18e83cad-da55-43ed-b6f5-11bf5210684f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 'AnnualCrop',\n",
       " 1: 'Forest',\n",
       " 2: 'HerbaceousVegetation',\n",
       " 3: 'Highway',\n",
       " 4: 'Industrial',\n",
       " 5: 'Pasture',\n",
       " 6: 'PermanentCrop',\n",
       " 7: 'Residential',\n",
       " 8: 'River',\n",
       " 9: 'SeaLake'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id2label = {k:v for k,v in enumerate(labels)}\n",
    "id2label"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aad7e9db-fed0-4e5c-8277-2c19b93a691b",
   "metadata": {},
   "source": [
    "上图图像的标签为'AnnualCrop'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8b9b91e3-1341-4727-ada4-ea521b53e140",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'AnnualCrop'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id2label[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b483c867-dd31-4f88-870a-ae1879ce7b80",
   "metadata": {},
   "source": [
    "### 加载模型\n",
    "从hub加载模型和处理器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "34617c66-b73d-40d1-9521-bb5ffeb64324",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Dumping model to file cache /tmp/jieba.cache\n",
      "Loading model cost 0.613 seconds.\n",
      "Prefix dict has been built successfully.\n",
      "100%|██████████| 266/266 [00:00<00:00, 1.38MB/s]\n",
      "1.04kB [00:00, 3.59MB/s]                 \n",
      "100%|██████████| 106M/106M [03:11<00:00, 583kB/s]  \n"
     ]
    }
   ],
   "source": [
    "from mindnlp.transformers.models import ConvNextForImageClassification,ConvNextImageProcessor\n",
    "img_processor = ConvNextImageProcessor.from_pretrained(\"nielsr/convnext-tiny-finetuned-eurosat\")\n",
    "model = ConvNextForImageClassification.from_pretrained(\"nielsr/convnext-tiny-finetuned-eurosat\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff23a9b4-d50f-4146-abad-7b4f56a4fe9a",
   "metadata": {},
   "source": [
    "我们取前边的例子进行测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e9193803-b77c-47af-b923-eb98bbb48e9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABAAEADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNO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R+x4QJ6NFErdpf//OFu9wN009tuS04gMtFpqEpXUR4NQbpVoC5/TXzFRfKxEiucSFIM5dJbtdk3QPTco8bAnzB1R8GyqmADFYUaGL+dDyAYpfogDm8AvNv2OTDGLYvGfRYuaV94a+mJr5iaECGyEAUZmsHM8jXvZyBBdGuiSQsQErFj5dDmMScRe2XtPEyswzmqXGdQ27YbefGlRHBUELwJIjiA0QXnjQnL1yhve6D41RfwDA/dIxrzabK7e5RDzcCYXRhTN0oufg0C0p9S8QGIBidCm6fEeL1NW6ND01Qa02UCMDQRcvqQ7VsiRB0aOyHkJhOKQo8/Vyw0HWBdyCmMqKMNdNUIUPzf9x++opka0AEAAAAAElFTkSuQmCC",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=64x64>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_image = example[\"image\"].convert(\"RGB\")\n",
    "test_image"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfccd8f1-448a-4024-8441-14b3e61e570c",
   "metadata": {},
   "source": [
    "### 图像预处理\n",
    "使用处理器对测试图像进行预处理，将其转换为张量，并提取预处理后的像素值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "63690316-692e-46ce-a1c9-c097a6d8cf2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 3, 224, 224)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pixel_values = img_processor(test_image,return_tensors=\"ms\").pixel_values\n",
    "pixel_values.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17b830ff-2687-46ab-8519-c9b2fbeb21f7",
   "metadata": {},
   "source": [
    "### 前向传播\n",
    "将预处理后的像素值输入到模型中进行前向传播，提取模型输出的logits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "16e514d2-ad87-4052-85b2-5327a198b268",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 10)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outputs = model(pixel_values)\n",
    "logits = outputs.logits\n",
    "logits.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cbf96ad-fc2b-4083-9ba1-938e32b2324e",
   "metadata": {},
   "source": [
    "### 预测类别\n",
    "根据logits得分，获取预测类别。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "877d5752-5052-4ada-b6e4-eceaca3ce1b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'AnnualCrop'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predicted_class_idx = logits.argmax(-1).item()\n",
    "model.config.id2label[predicted_class_idx]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python-3.9.0",
   "language": "python",
   "name": "python-3.9.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
